//
//  NYMineHeaderView.m
//  NYTYY
//
//  Created by MinLison on 2017/12/8.
//  Copyright © 2017年 NYtyy. All rights reserved.
//

#import "NYMineHeaderView.h"
@interface NYMineHeaderView ()
@property(nonatomic, strong, readwrite) UIImageView *backGroundView;
@property(nonatomic, strong) QMUIGhostButton *headImgBtn;
@property(nonatomic, strong) TTTAttributedLabel *nickNamelabel;
@property(nonatomic, strong) QMUIGhostButton *tipsButton;
@property(nonatomic, strong) QMUIButton *checkInBtn;
@property(nonatomic, strong) UIStackView *stackView;
@property(nonatomic, strong) QMUIButton *settingButton;

@end
@implementation NYMineHeaderView

- (void)setupView
{
        [super setupView];
        // 104 + 37
        [self addSubview:self.backGroundView];
        [self addSubview:self.headImgBtn];
        UIStackView *stackView = [[UIStackView alloc] initWithArrangedSubviews:@[self.nickNamelabel,self.tipsButton]];
        self.stackView = stackView;
        stackView.axis = UILayoutConstraintAxisVertical;
        stackView.distribution = UIStackViewDistributionEqualSpacing;
        stackView.alignment = UIStackViewAlignmentCenter;
        stackView.spacing = __WGHeight(10);
        [self addSubview:stackView];
        [self addSubview:self.checkInBtn];
        [self addSubview:self.settingButton];
        
        [self.tipsButton mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(__WGHeight(20));
                make.width.mas_equalTo(__WGWidth(91));
        }];
        [self.backGroundView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(self);
        }];
        [self.headImgBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.mas_top).offset(__WGHeight(75));
                make.centerX.equalTo(self);
                make.height.width.mas_equalTo(__WGWidth(55));
        }];
        [stackView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.headImgBtn.mas_bottom).offset(__WGWidth(12));
                make.centerX.equalTo(self.headImgBtn);
        }];
        [self.checkInBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self.mas_right).offset(-__WGWidth(44));
                make.top.equalTo(self.mas_top).offset(__WGHeight(92));
                make.height.width.mas_equalTo(__WGWidth(70));
        }];
        
//        self.settingButton.backgroundColor = [UIColor yellowColor];
        [self.settingButton mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.mas_top).offset(__WGHeight(64));
                make.right.equalTo(self.mas_right).offset(-__WGWidth(15));
                make.width.equalTo(@40);
                make.height.equalTo(@40);
        }];
        if ([[UIDevice currentDevice].model isEqualToString: @"iPad"]){
                [self.settingButton mas_remakeConstraints:^(MASConstraintMaker *make) {
                        make.top.equalTo(self.mas_top).offset(__WGHeight(114));
                        make.right.equalTo(self.mas_right).offset(-__WGWidth(0));
                        make.width.equalTo(@30);
                        make.height.equalTo(@40);
                }];
        }
        self.settingButton.qmui_outsideEdge = UIEdgeInsetsMake(-20, -20, -20, -20);
        
        @weakify(self);
        [self.checkInBtn jk_addActionHandler:^(NSInteger tag) {
                @strongify(self);
                if (self.actionBlock) {
                        self.actionBlock(NYMineHeaderViewClickTypeCheckIn);
                }
        }];
        
        [self updateContent];
        [self.KVOController observe:NYUserManager keyPath:@keypath(NYUserManager,userInfoDidChange) options:(NSKeyValueObservingOptionNew) block:^(NYMineHeaderView *  _Nullable observer, NYUser *  _Nonnull object, NSDictionary<NSString *,id> * _Nonnull change) {
                [observer updateContent];
        }];
        [self.KVOController observe:NYUserManager keyPath:@keypath(NYUserManager,isSignIn) options:(NSKeyValueObservingOptionNew) block:^(NYMineHeaderView *  _Nullable observer, id  _Nonnull object, NSDictionary<NSString *,id> * _Nonnull change) {
                [observer updateContent];
        }];
        [self.headImgBtn jk_addActionHandler:^(NSInteger tag) {
                @strongify(self);
                if (self.actionBlock) {
                        self.actionBlock(NYMineHeaderViewClickTypeHeadImgClick);
                }
        }];
        [self.settingButton jk_addActionHandler:^(NSInteger tag) {
                @strongify(self);
                if (self.actionBlock) {
                        self.actionBlock(NYMineHeaderViewClickTypeSettingClick);
                }
        }];
        [self jk_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
                if (self.actionBlock && !NYUserManager.isLogin) {
                        self.actionBlock(NYMineHeaderViewClickTypeHeadImgClick);
                }
        }];
        [self.tipsButton jk_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
                if (self.actionBlock && !NYUserManager.isLogin) {
                        self.actionBlock(NYMineHeaderViewClickTypeHeadImgClick);
                }
        }];
}
- (void)updateContent
{
        [self.headImgBtn sd_setImageWithURL:[NSURL URLWithString:NOT_NULL_STRING_DEFAULT_EMPTY(NYUserManager.avatar)] forState:(UIControlStateNormal) placeholderImage:[UIImage pic_default_avatar]];
        [self.tipsButton setTitle:NOT_NULL_STRING(NYUserManager.levelModel.level, @"---") forState:(UIControlStateNormal)];
        NSString *nameStr = NOT_NULL_STRING(NYUserManager.nickname, @"匿名同学");
        self.nickNamelabel.text = NYUserManager.isLogin ? [NSString stringWithFormat:@"%@",nameStr] : @"注册/登录";
        self.checkInBtn.enabled = !NYUserManager.isSignIn;
}
- (QMUIButton *)checkInBtn
{
        if (_checkInBtn == nil) {
                _checkInBtn = [[QMUIButton alloc] qmui_initWithImage:[UIImage da_qia_qian_dao] title:nil];
                [_checkInBtn setImage:[UIImage da_qian_qian_dao_disable] forState:(UIControlStateDisabled)];
        }
        return _checkInBtn;
}

- (QMUIGhostButton *)tipsButton
{
        if (_tipsButton == nil) {
                _tipsButton = [[QMUIGhostButton alloc] init];
                _tipsButton.ghostColor = [UIColor whiteColor];
                _tipsButton.borderWidth = 2;
                _tipsButton.cornerRadius = 10;
                _tipsButton.clipsToBounds = YES;
                _tipsButton.titleLabel.font = WGSystem10Font;
                [_tipsButton setTitleColor:UIColorHex(0x2DA0FF) forState:(UIControlStateNormal)];
                _tipsButton.adjustsButtonWhenDisabled = NO;
                _tipsButton.adjustsImageWhenDisabled = NO;
                [_tipsButton setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:(UIControlStateNormal)];
        }
        return _tipsButton;
}

- (TTTAttributedLabel *)nickNamelabel
{
        if (_nickNamelabel == nil) {
                _nickNamelabel = [self createLabel];
                _nickNamelabel.textAlignment = NSTextAlignmentCenter;
        }
        return _nickNamelabel;
}
- (TTTAttributedLabel *)createLabel
{
        TTTAttributedLabel *label = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
        label.font = WGSystem16Font;
        label.textColor = [UIColor whiteColor];
        return label;
}
- (QMUIGhostButton *)headImgBtn
{
        if (_headImgBtn == nil) {
                _headImgBtn = [[QMUIGhostButton alloc] init];
                _headImgBtn.ghostColor = [UIColor whiteColor];
                _headImgBtn.borderWidth = 2;
                _headImgBtn.clipsToBounds = YES;
        }
        return _headImgBtn;
}
- (QMUIButton *)settingButton
{
        if (_settingButton == nil) {
                _settingButton = [[QMUIButton alloc] qmui_initWithImage:[UIImage she_zhi] title:nil];
        }
        return _settingButton;
}
- (UIImageView *)backGroundView
{
        if (_backGroundView == nil) {
                _backGroundView = [[UIImageView alloc] initWithImage:[UIImage bei_jing]];
        }
        return _backGroundView;
}
@end
